import axios from 'axios'
import settings from '@/settings'
import { reactive } from 'vue'
import { ElNotification } from 'element-plus'
import { usePermissionStoreHook } from "@/store/permission"
import storage from '@/utils/storage'

// create an axios instance
const service = axios.create({
  baseURL: settings.baseUrl, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})


// request interceptor
service.interceptors.request.use(
  config => {
    // do something before request is sent
   // debugger
    if (storage.get('accessToken')) {
      // let each request carry token
      // ['X-Token'] is a custom headers key
      // please modify it according to the actual situation
      config.headers['authorization'] = `${storage.get('tokenType')} ${storage.get('accessToken')}`
    }
    return config
  },
  error => {
    // do something with request error
    console.log(error) // for debug
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data
    // if the custom code is not 20000, it is judged as an error.
    if(res instanceof Blob) {

    }
    else if(res.success) {

    }
    else if (!res.success) {
      ElNotification({
        message: res.message || 'Error',
        type: 'error',
      })
    }

    return Promise.resolve(res)
  },
  err => {
    console.log('err' + err) // for debug
    if (err && err.response) {
      switch (err.response.status) {
        case 400:
          err.message = '请求错误'
          break

        case 401:
          err.message = '未授权，请登录'
          break

        case 403:
          err.message = '拒绝访问'
          break

        case 404:
          err.message = `请求地址出错: ${err.response.config.url}`
          break

        case 408:
          err.message = '请求超时'
          break

        case 500:
          err.message = '服务器内部错误'
          break

        case 501:
          err.message = '服务未实现'
          break

        case 502:
          err.message = '网关错误'
          break

        case 503:
          err.message = '服务不可用'
          break

        case 504:
          err.message = '网关超时'
          break

        case 505:
          err.message = 'HTTP版本不受支持'
          break

        default:
      }

      ElNotification({
        message: err.message,
        type: 'error',
      })

      return Promise.reject(err)
    }
  }
)

async function useFetch(config) {
  const result = ref(null)
  const success = ref(null)
  const message = ref(null)
  const code = ref(null)

  const currentPage = ref(null)
  const pageSize = ref(null)
  const total = ref(null)


  const output = reactive({})

  async function doFetch() {
    const res = await service(config)

    success.value = res.data.success
    message.value = res.data.message
    code.value = res.data.code

    if (res.data.result.hasOwnProperty('data')) {
      result.value = res.data.result.data
      currentPage.value = res.data.result.currentPage
      pageSize.value = res.data.result.pageSize
      total.value = res.data.result.total
    }
    else {
      result.value = res.data.result
    }
  }

  await doFetch()

  return { result, success, message, code, currentPage, pageSize, total }
}


export default service
